var testData1 = {
  title: {
    icon: {
      options: []
    },
    iconColor: {
      options: []
    }
  },
  layout: {
    nodeIcon: {
      options: []
    },
    nodeIconColor: {
      options: []
    }
  },
  instance: {
    titleIcon: {
      options: []
    },
    titleIconColor: {
      options: []
    },
    sumIcon: {
      options: []
    },
    sumIconColor: {
      options: []
    }
  },
  child: {
    icon: {
      options: [],
      value: ''
    },
    iconColor: {
      options: [],
      value: ''
    }
  }
}

var testData2 = [
  {
    basic: {
      icon: {
        options: []
      },
      iconColor: {
        options: []
      }
    }
  }
]

/**
 * 获取数据，模拟ajax请求
 */
var getData = function() {
  return new Promise((resolve, reject) => {
    resolve({
      'icons': [{
        name: 'company1',
        url: 'xxx1'
      }, {
        name: 'company2',
        url: 'xxx2'
      }, {
        name: 'company3',
        url: 'xxx2'
      }],
      'colors': [{
        name: 'red',
        color: 'red'
      }, {
        name: 'green',
        color: 'green'
      }, {
        name: 'blue',
        color: 'blue'
      }]
    })
  })
}

// 场景一
var scenario1 = function() {
  getData().then(function(data) {
    var titleObj = testData1.title
    var layoutObj = testData1.layout
    titleObj.icon.options = data.icons
    titleObj.iconColor.options = data.colors
    layoutObj.nodeIcon.options = data.icons
    layoutObj.nodeIconColor.options = data.colors
  })
}

// 场景二
var scenario2 = function() {
  getData().then(function(data) {
    var instanceObj = testData1.instance
    instanceObj.titleIcon.options = data.icons
    instanceObj.titleIconColor.options = data.colors
    instanceObj.sumIcon.options = data.icons
    instanceObj.sumIconColor.options = data.colors
  })
}

// 场景三
var scenario3 = function() {
  getData().then(function(data) {
    var childObj = testData1.child
    childObj.icon.options = data.icons
    childObj.icon.value = 'noIcon'
    childObj.iconColor.options = data.icons
    childObj.iconColor.value = 'noIcon'
  })
}

// 场景四
var scenario4 = function() {
  getData().then(function(data) {
    const basicObj = testData2[0].basic
    basicObj.icon.options = data.icons
    basicObj.iconColor.options = data.icons
  })
}

scenario1()
scenario2()
scenario3()
scenario4()

//

function baseData(options = [], value = '') {
  return {
    options,
    value
  }
}

const testData1 = {
  title: {
    icon: baseData(),
    iconColor: baseData()
  },
  layout: {
    nodeIcon: baseData(),
    nodeIconColor: baseData()
  },
  instance: {
    titleIcon: baseData(),
    titleIconColor: baseData(),
    sumIcon: baseData(),
    sumIconColor: baseData()
  },
  child: {
    icon: baseData(),
    iconColor: baseData()
  }
}

const testData2 = [
  {
    basic: {
      icon: baseData(),
      iconColor: baseData()
    }
  }
]

/**
 * 获取数据，模拟ajax请求
 */
const getData = function() {
  return new Promise((resolve, reject) => {
    resolve({
      'icons': [{
        name: 'company1',
        url: 'xxx1'
      }, {
        name: 'company2',
        url: 'xxx2'
      }, {
        name: 'company3',
        url: 'xxx2'
      }],
      'colors': [{
        name: 'red',
        color: 'red'
      }, {
        name: 'green',
        color: 'green'
      }, {
        name: 'blue',
        color: 'blue'
      }]
    })
  })
}

function setIconAndColorOptions(obj, field, data) {
  if (obj[field]) {
    obj[field].options = data.icons
  }
  const colorField = field + 'Color'
  if (obj[colorField]) {
    obj[colorField].options = data.colors
  }
}

function setIconOptionsAndValue(obj, field, data, value = null) {
  if (obj[field]) {
    obj[field].options = data.icons
    if (value !== null) {
      obj[field].value = value
    }
  }
  const colorField = field + 'Color'
  if (obj[colorField]) {
    obj[colorField].options = data.icons
    if (value !== null) {
      obj[colorField].value = value
    }
  }
}

// 场景一
var scenario1 = function() {
  getData().then(function(data) {
    setIconAndColorOptions(testData1.title, 'icon', data)
    setIconAndColorOptions(testData1.layout, 'nodeIcon', data)
  })
}

// 场景二
var scenario2 = function() {
  getData().then(function(data) {
    setIconAndColorOptions(testData1.instance, 'titleIcon', data)
    setIconAndColorOptions(testData1.instance, 'sumIcon', data)
  })
}

// 场景三
var scenario3 = function() {
  getData().then(function(data) {
    setIconOptionsAndValue(testData1.child, 'icon', data, 'noIcon')
  })
}

// 场景四
var scenario4 = function() {
  getData().then(function(data) {
    // const basicObj = testData2[0].basic
    // basicObj.icon.options = data.icons
    // basicObj.iconColor.options = data.icons

    setIconOptionsAndValue(testData2[0].basic, 'icon', data)
  })
}
